package net 
{
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.net.registerClassAlias;
	import flash.utils.Timer;
	import net.msg.MsgHeartBeat;
	/**
	 * ...
	 * @author zxx
	 */
	public class NetManager
	{
		private static var _Socket:NetClient;
		private static var HeartBeat:Timer;
		private static var _MsgHeartBeat:MsgHeartBeat;
		public static function get Socket():NetClient
		{
			if (_Socket == null)
			{
				_Socket = new NetClient();
			}
			
			return _Socket;
		}
		public function NetManager() 
		{
			
		}
		private static function OnHeartBeat(e:Event):void
		{
			if (Socket.Connected)
			{
				Socket.SendMessage(_MsgHeartBeat);
			}
		}
		
		public static function Init(Ip:String,Port:int):Boolean
		{
			if (!Socket.Open(Ip, Port))
				return false;
			RegisterCallback();
			RegisterClass();
			return true;
		}
		
		private static function RegisterCallback():void
		{
			HeartBeat = new Timer(30 * 1000);
			HeartBeat.addEventListener(TimerEvent.TIMER, OnHeartBeat);
			HeartBeat.start();
			//todo...
			//注册回调函数。
			//连接事件
			_Socket.OnConnected =  map.GetInstance().OnConnected;
			//断开连接事件
			_Socket.OnDisconnected = map.GetInstance().OnDisconnected;
			
			//各个消息包事件。
			_Socket.RegisterMessageCallback(2, map.GetInstance().DoMapInfo2);
			_Socket.RegisterMessageCallback(3, map.GetInstance().DoPathInfo);
			_Socket.RegisterMessageCallback(5, map.GetInstance().DoActorsPosition);
			_Socket.RegisterMessageCallback(6, map.GetInstance().DoRemoveUser);
		}
		
		private static function RegisterClass():void
		{
			//todo...
			//注册服务端类名称。
			registerClassAlias("test46.MapInfo2", MapInfo2);
			registerClassAlias("test46.UserInfo", UserInfo);
			registerClassAlias("AMFNet.MessageBase", MessageBase);
			registerClassAlias("test46.PathInfo", PathInfo);
			registerClassAlias("test46.PathInfo2", PathInfo2);
			registerClassAlias("test46.ActorsPosition", ActorsPosition); 
			registerClassAlias("test46.RemoveUser", RemoveUser);
		}
	}

}